#!/usr/bin/env bash
# Configure jenkins service before startup

SVC_NAME="application/jenkins:default"

ARG_NAME=http_port
if /usr/sbin/mdata-get ${ARG_NAME} 1>/dev/null 2>&1; then    
    ARG_VALUE=$(/usr/sbin/mdata-get ${ARG_NAME})
    if [ "${ARG_VALUE}" != "8080" ]; then
        svccfg -s ${SVC_NAME} setprop config/http_port = astring: "${ARG_VALUE}"
        gsed -i "s/:8080/:${ARG_VALUE}/g" /opt/local/etc/nginx/nginx.conf
        svcadm refresh ${SVC_NAME}
    fi
fi

log "starting jenkins"

svcadm enable ${SVC_NAME}

ARG_NAME=update_center_url
if /usr/sbin/mdata-get ${ARG_NAME} 1>/dev/null 2>&1; then    
    ARG_VALUE=$(/usr/sbin/mdata-get ${ARG_NAME}) 
    cat > /home/jenkins/.jenkins/hudson.model.UpdateCenter.xml << EOF
<?xml version='1.1' encoding='UTF-8'?>
<sites>
  <site>
    <id>default</id>
    <url>${ARG_VALUE}</url>
  </site>
</sites>
EOF
   svcadm restart ${SVC_NAME}
fi 
